package com.ssy.lingxi.product.controller.pc.platform;

import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.model.vo.PageVO;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.product.api.model.request.CategoryAttributeRequest;
import com.ssy.lingxi.product.api.model.request.CategoryAttributeSortRequest;
import com.ssy.lingxi.product.api.model.response.AttributeResponse;
import com.ssy.lingxi.product.entity.platform.Attribute;
import com.ssy.lingxi.product.service.platform.ICategoryAttributeService;
import org.modelmapper.TypeToken;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @menu: 平台后台--品类关联属性
 * 平台后台--品类关联属性管理类
 * @author jw.chen
 * @version 2.0.0
 * @date 2020/6/23
 */
@RestController
@RequestMapping("/product/platform")
public class CategoryAttributeController extends BaseController {

    @Resource
    private ICategoryAttributeService CategoryAttributeService;

    /**
     * 查询品类下的属性列表
     * @param pageVO 分页实体
     * @param name 属性名称
     * @param categoryId 品类id
     * @param isByCategory 是否查询该品类下的属性
     * @return
     */
    @RequestMapping(value = "/getCategoryAttributeList", method = RequestMethod.GET)
    public Wrapper<PageData<AttributeResponse>> getCategoryAttributeList(PageVO pageVO, @RequestParam(value = "name", required = false) String name, @RequestParam(value = "groupName", required = false) String groupName, @RequestParam(value = "categoryId") Long categoryId, @RequestParam("isByCategory") Boolean isByCategory) {
        Page<Attribute> result = CategoryAttributeService.getCategoryAttributeList(pageVO, name, groupName, categoryId, isByCategory);
        List<AttributeResponse> resultList = this.modelMapper.map(result.getContent(), new TypeToken<List<AttributeResponse>>(){}.getType());
        return Wrapper.success(new PageData<>(result.getTotalElements(), resultList));
    }

    /**
     * 查询品类下的属性列表-MRO模式 按照特定的排序返回结果
     *
     * @param name         属性名称
     * @param categoryId   品类id
     * @param isByCategory 是否查询该品类下的属性
     * @return
     */
    @RequestMapping(value = "/getMroCategoryAttributeList", method = RequestMethod.GET)
    public Wrapper<List<Attribute>> getMroCategoryAttributeList(@RequestParam(value = "name", required = false) String name, @RequestParam(value = "groupName", required = false) String groupName, @RequestParam(value = "categoryId") Long categoryId, @RequestParam("isByCategory") Boolean isByCategory) {
        return Wrapper.success(CategoryAttributeService.getMroCategoryAttributeList(name, groupName, categoryId, isByCategory));
    }

    /**
     * 新增/修改品类下的属性
     * @param categoryAttributeRequest
     * @return
     */
    @RequestMapping(value = "/saveCategoryAttribute", method = RequestMethod.POST)
    public Wrapper<String> saveCategoryAttribute(@RequestBody CategoryAttributeRequest categoryAttributeRequest){
        return Wrapper.success(CategoryAttributeService.saveCategoryAttribute(categoryAttributeRequest));
    }

    /**
     * 解除品类属性关系
     * @param categoryAttributeRequest
     * @return
     */
    @RequestMapping(value = "deleteCategoryAttribute", method = RequestMethod.POST)
    public Wrapper<String> deleteCategoryAttribute(@RequestBody CategoryAttributeRequest categoryAttributeRequest){
        return Wrapper.success(CategoryAttributeService.deleteCategoryAttribute(categoryAttributeRequest));
    }

    /**
     * 新增/修改品类下的属性排序
     *
     * @param categoryAttributeSortRequestList
     * @return
     */
    @PostMapping("/saveCategoryAttributeSort")
    public Wrapper<Boolean> saveCategoryAttributeSort(@RequestBody List<CategoryAttributeSortRequest> categoryAttributeSortRequestList) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(CategoryAttributeService.saveCategoryAttributeSort(sysUser, categoryAttributeSortRequestList));
    }

}
